import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/index.js'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      component: () => import('@/views/layout/LayoutContainer.vue'),
      redirect: '/overview/overview',
      children: [
        {
          path: '/overview/overview',
          component: () => import('@/views/overview/OverviewPage.vue'),
          meta: {
            title: '总览信息'
          }
        },
        {
          path: '/config/carousel',
          component: () => import('@/views/config/CarouselPage.vue'),
          meta: {
            title: '轮播图配置'
          }
        },
        {
          path: '/config/hot-sale',
          component: () => import('@/views/config/HotSalePage.vue'),
          meta: {
            title: '热销商品配置'
          }
        },
        {
          path: '/config/new-goods',
          component: () => import('@/views/config/NewGoodsPage.vue'),
          meta: {
            title: '新品上线配置'
          }
        },
        {
          path: '/config/recommended',
          component: () => import('@/views/config/RecommendedPage.vue'),
          meta: {
            title: '为你推荐配置'
          }
        },
        {
          path: '/model/category',
          component: () => import('@/views/manage/CategoryPage.vue'),
          meta: {
            title: '分类管理'
          }
        },
        {
          path: '/model/goods',
          component: () => import('@/views/manage/GoodsPage.vue'),
          meta: {
            title: '商品管理'
          }
        },
        {
          path: '/model/order',
          component: () => import('@/views/manage/OrderPage.vue'),
          meta: {
            title: '订单管理'
          }
        },
        {
          path: '/model/details/:orderNo',
          component: () => import('@/views/manage/OrderDetails.vue'),
          meta: {
            title: '订单详情'
          }
        },
        {
          path: '/model/user',
          component: () => import('@/views/manage/UserPage.vue'),
          meta: {
            title: '用户管理'
          }
        },
        {
          path: '/system/admin',
          component: () => import('@/views/system/AdminPage.vue'),
          meta: {
            title: '员工管理'
          }
        },
        {
          path: '/system/log',
          component: () => import('@/views/system/LogPage.vue'),
          meta: {
            title: '员工管理'
          }
        },
        {
          path: '/system/my-info',
          component: () => import('@/views/system/MyInfoPage.vue'),
          meta: {
            title: '我的信息'
          }
        },
        {
          path: '/overview/add-goods',
          component: () => import('@/views/goods/AddGoods.vue'),
          meta: {
            title: '添加商品'
          }
        }
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/login/LoginPage.vue')
    }
  ]
})

// 配置路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  if (to.path === '/login') {
    next()
  } else {
    if (userStore.token) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
